** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_C4.smcl", replace

use "$project_path/data/4_regdata/dynamics/regdata_dynamics.dta", clear
keep if relative_year==-3

// Generating aggregate indices
foreach v in growth_gdpc_penn inflation_imf unemployment_ilo trade_wb hdi democracy_index {
	egen std_`v' = std(`v')
}
gen mstd_inflation_imf = -std_inflation_imf
gen mstd_unemployment_ilo = -std_unemployment_ilo
egen chix_econ = rowmean(std_growth_gdpc_penn mstd_inflation_imf mstd_unemployment_ilo std_trade_wb)
egen std_chix_econ = std(chix_econ)
egen general_chix = rowmean(std_chix_econ std_hdi std_democracy_index)

rdrobust chix_econ runvar, all
rdrobust std_growth_gdpc_penn runvar, all
rdrobust mstd_inflation_imf runvar, all
rdrobust mstd_unemployment_ilo runvar, all
rdrobust std_trade_wb runvar, all
rdrobust std_hdi runvar, all
rdrobust std_democracy_index runvar, all
rdrobust general_chix runvar, all

log close

** Building .tex table

#delimit ;
local outcomes  
		chix_econ 
		std_growth_gdpc_penn 
		mstd_inflation_imf 
		mstd_unemployment_ilo
		std_trade_wb 
		std_hdi 
		std_democracy_index 
		general_chix 
;
#delimit cr		

local model_count : word count `outcomes'

	// Initialize matrix of results and define value labels
	local ncols = `model_count'*2
	local nrows = 6
	//Prepare matrix of coefficients and other statistics
	mat Coefs = J(`nrows', `ncols', .)
		

//inizialize indices
local i = 0
local pval_col = `model_count'
foreach v in `outcomes'{
	local ++i 
	local ++pval_col
	rdrobust `v' runvar, all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	local N_`i': di %6.0f e(N)
	loca N_eff_`i': di %6.0f e(N_h_l) + e(N_h_r)
	local bw_`i': di %5.1f e(h_r)
	

	
	mat Coefs[1,`i'] = `b_`i''
	mat Coefs[2,`i'] = `se_`i''
	mat Coefs[3,`i'] = `p_`i''	
	mat Coefs[4,`i'] = `N_`i''
	mat Coefs[5,`i'] = `N_eff_`i''
	mat Coefs[6,`i'] = `bw_`i''
	
	mat Coefs[1,`pval_col'] = `p_`i''
	


}
mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
	// Clear the dataset, and use the matrix as dataset			

	clear
	svmat Coefs

	//	input variable names
	input str22 varname 
	"El. turn."
	"SE"
	"p-val"
	"N"
	"N eff."
	"Band."
	
	replace varname = "" if varname == "SE"
	order varname
	
	// Rename cols and labels
	forvalues n = 1/`model_count'{
		rename Coefs`n' Model`n'
	}
	
	//Cols where we store pvalues
	local n_min = `model_count' + 1
	local n_max = `model_count'*2
	local i = 1
	forvalues n = `n_min'/`n_max'{
		rename Coefs`n' P_Model`i'
		local ++i
	}

	label var varname ""
	label var Model1 "\textbf{Econ. perf.}"
	label var Model2 "GDP p.c. gr."
	label var Model3 "(Minus) Inflation"
	label var Model4 "(Minus) Unemp."
	label var Model5 "Trade"
	label var Model6 "\textbf{HDI}"
	label var Model7 "\textbf{Democ.}"
	label var Model8 "\textbf{General index}"
		
	// Convert to string and add options
	forvalues i = 1/`model_count'{
		tostring Model`i', replace force format(%04.3f)
		replace Model`i'= "" if Model`i'=="."
		//Add parenthesis to SE and P-val
		replace Model`i' = "(" + Model`i' + ")" if missing(varname) & Model`i' !=""	
		replace Model`i' = "[" + Model`i' + "]" if varname == "p-val" & Model`i' !=""	
		//Remove decimals to N obs
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")-1) if varname == "N"
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")-1) if varname == "N eff."
		
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")+1) if varname == "Band."

	}
	
	// Add stars
	local 3_star 0.01
	local 2_star 0.05
	local 1_star 0.10
	
	forvalues i = 1/8{
		
		replace Model`i' = Model`i' + "***" if P_Model`i' < `3_star'
		replace Model`i' = Model`i' + "**" if P_Model`i' < `2_star' & P_Model`i' > `3_star'
		replace Model`i' = Model`i' + "*" if P_Model`i' < `1_star' & P_Model`i' > `2_star'
		
		drop P_Model`i'
		
	}

	local title "Placebo tests: Effects on the level of the outcome variables three years before the election"
	local label_key "tab:placebo_baseline_level_year_m3"
	local output "$project_path/output/appendix_tables/Appendix_Table_C4.tex"
	local header "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)"
	local footnote = "\emph{Notes}: This table reports estimates for equation (\ref{eq:baseline_rdd}), using as a dependent variable the value of the outcomes three years before the election."

	texsave using "`output'", replace varlabels frag nofix hlines(3) location(h) ///
							  title("`title'")  ///
							  footnote("`footnote'") ///
							  marker("`label_key'") ///
							  headerlines("`header'")
			
